package com.istock.union.user.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.istock.base.common.api.model.PageInfo;
import com.istock.base.export.util.DataTranslateUtils;
import com.istock.base.mybatis.utils.MybatisFieldConvertUtils;
import com.istock.union.user.dao.UserOnlineInfoMapper;
import com.istock.union.user.model.OrderByModel;
import com.istock.union.user.model.UserOnlineInfo;
import com.istock.union.user.model.UserOnlineInfoExample;
import com.istock.union.user.service.UserOnlineInfoService;
import com.istock.union.user.translate.DeptTranslateCallback;
import com.istock.union.user.translate.TenantTranslateCallback;

@Repository
public class UserOnlineInfoServiceImpl implements UserOnlineInfoService {

	@Autowired
	private UserOnlineInfoMapper mapper;
	
	public void insertUserOnline(UserOnlineInfo userOnlineInfo){
		mapper.insertSelective(userOnlineInfo);
	}
	
	public void removeUserOnline(String sessionId) {
		UserOnlineInfoExample example = new UserOnlineInfoExample();
		UserOnlineInfoExample.Criteria criteria = example.createCriteria();
		criteria.andSessionIdEqualTo(sessionId);
		mapper.deleteByExample(example);
	}
	
	public List search(String loginName, List<String> deptList, String ipaddr, PageInfo pageInfo , OrderByModel orderByModel){
		UserOnlineInfoExample example = new UserOnlineInfoExample();
		UserOnlineInfoExample.Criteria criteria = example.createCriteria();

		if(deptList != null && !deptList.isEmpty()) {
			criteria.andDeptCodeIn(deptList);
		}
		
		if (StringUtils.isNotBlank(ipaddr)) {
			criteria.andIpaddrLike("%" + ipaddr + "%");
		}
		if (StringUtils.isNotBlank(loginName)) {
			criteria.andLoginNameLike("%" + loginName + "%");
		}
		if(orderByModel == null) {
			example.setOrderByClause("LAST_ACCESS_TIME DESC");
		}else {
			example.setOrderByClause(MybatisFieldConvertUtils.getInstance().unmarshal("com.istock.union.user.dao.UserOnlineInfoMapper", "BaseResultMap", orderByModel.getOrderByColumn())+" "+orderByModel.getIsAsc());
		}
		
		
		mapper.selectByExample(example, pageInfo);
		
		List<Map> list = DataTranslateUtils.translate(pageInfo.getData(), "deptCode", "deptName", new DeptTranslateCallback());
		list = DataTranslateUtils.translate(list, "tenantCode", "tenantName", new TenantTranslateCallback());
		pageInfo.setData(list);
		
		return list;
	}
	
	/**在经过check以后的sessionId,如果在缓存里面不存在,则进行删除
	 * 
	 * @param sessionId
	 */
	public void remveUserOnlineInfo(String[] sessionIds){
		if(sessionIds != null){
			UserOnlineInfoExample example = new UserOnlineInfoExample();
			UserOnlineInfoExample.Criteria criteria = example.createCriteria();
			
			criteria.andSessionIdIn(Arrays.asList(sessionIds));
			mapper.deleteByExample(example);
		}
	}

	/**
	 * 根据时间范围和状态查询
	 * 
	 * @param less
	 * @param greater
	 * @param statusList
	 * @return
	 */
	public List<UserOnlineInfo> selectUserOnline(Integer less, Integer greater, List<Integer> statusList) {
		UserOnlineInfoExample example = new UserOnlineInfoExample();
		UserOnlineInfoExample.Criteria criteria = example.createCriteria();
		Date greaterThan = null;
		Date lessThan = null;
		if (null != less) {
			greaterThan = DateUtils.addMinutes(new Date(), 0-less);
			criteria.andLastAccessTimeGreaterThanOrEqualTo(greaterThan);
		}
		if (null != greater) {
			lessThan = DateUtils.addMinutes(new Date(), greater);
			criteria.andLastAccessTimeLessThan(lessThan);
		}
		
		return mapper.selectByExample(example);
	}

	@Override
	public void updateUserOnline(String sessionId , UserOnlineInfo userOnlineInfo) {
		UserOnlineInfoExample example = new UserOnlineInfoExample();
		UserOnlineInfoExample.Criteria criteria = example.createCriteria();
		criteria.andSessionIdEqualTo(sessionId);
		mapper.updateByExampleSelective(userOnlineInfo, example);
	}
	
	/**
	 * 清除在线用户里面的超时token
	 */
	public int clearExpireToken() {
		UserOnlineInfoExample example = new UserOnlineInfoExample();
		UserOnlineInfoExample.Criteria criteria = example.createCriteria();
		criteria.andExpireTimeLessThanOrEqualTo(new Date());
		return mapper.deleteByExample(example);
	}
}
